using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

//We need to add this "using"
using Module_Persistence;
using Module_Basic_Entities;

namespace ExampleForUsing
{
    /// <summary>
    /// 
    /// It's an Example!
    /// 
    /// Developed by lucianoJose:
    /// -Game Developer at Manifesto Game Studio(http://manifestogames.com.br/)
    /// -Administrator of the Brazilian XNA Community - SharpGames(http://www.sharpgames.net)
    /// 
    /// For more informations about me: lucianojosefj.spaces.live.com    
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        private SpriteBatch spriteBatch;

        private Map2D map2D;

        private SpriteFont spriteFont;

        /// <summary>
        /// List of tiles intersected by the Avatar that is controlled by the Mouse Pointer.
        /// </summary>
        private List<Tile> intersectedTiles;

        /// <summary>
        /// Tile pointed by the Mouse
        /// </summary>
        private Tile? pointedTile;

        private string tileInformations = "";
        private string blockInformations = "";

        private Texture2D avtText;
        private Vector2 avtPos;

        private int curLayer = 0;

        KeyboardState curKeyboardState, lastKeyboardState;

        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            this.Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            this.spriteBatch = new SpriteBatch(GraphicsDevice);

            this.map2D = this.Content.Load<Map2D>("TestLevel1");

            this.spriteFont = this.Content.Load<SpriteFont>("SpriteFont1");

            this.avtText = this.Content.Load<Texture2D>("avatar");
        }



        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
                || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            curKeyboardState = Keyboard.GetState();

            MouseState mouseState = Mouse.GetState();

            avtPos.X = mouseState.X;
            avtPos.Y = mouseState.Y;

            Layer2D layer = map2D.GetLayerAt(curLayer) as Layer2D;

            if (layer != null)
            {

                pointedTile = layer.ContainsTile(new Vector2(avtPos.X, avtPos.Y));

                if (pointedTile != null)
                {
                    this.tileInformations = pointedTile.Value.ToString();

                    this.blockInformations = layer.GetBlockInformation(pointedTile.Value.Id).ToString();
                }
                else
                {
                    this.tileInformations = "";
                    this.blockInformations = "";
                }

                this.intersectedTiles = layer.IntersectTiles(
                    new Rectangle(
                        (int)avtPos.X,
                        (int)avtPos.Y,
                        this.avtText.Width,
                        this.avtText.Height)
                        );
                
            }

            if (curKeyboardState.IsKeyDown(Keys.Left) && lastKeyboardState.IsKeyUp(Keys.Left))
            {
                curLayer--;

                if (curLayer < 0)
                    curLayer = 0;

            }

            if (curKeyboardState.IsKeyDown(Keys.Right) && lastKeyboardState.IsKeyUp(Keys.Right))
            {
                curLayer++;

                if (curLayer >= map2D.NumChildLayers)
                {
                    curLayer = map2D.NumChildLayers - 1;
                }
            }

            lastKeyboardState = curKeyboardState;

            

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            this.graphics.GraphicsDevice.Clear(Color.Black);

            string text = "Pointed Tile by the Mouse:";

            int offSetY = 30;

            this.spriteBatch.Begin();

            this.spriteBatch.Draw((float)gameTime.ElapsedGameTime.TotalSeconds, this.map2D, Vector2.Zero, Color.White);
            
            this.spriteBatch.DrawString(this.spriteFont,
                text, new Vector2(540, offSetY), Color.White);

            offSetY += (int)spriteFont.MeasureString(text).Y;

            this.spriteBatch.DrawString(this.spriteFont,
                this.tileInformations, new Vector2(580, offSetY), Color.Pink);

            this.spriteBatch.DrawString(this.spriteFont,
                this.blockInformations,
                new Vector2(580, spriteFont.MeasureString(this.tileInformations).Y + 35),
                Color.White);
            
            offSetY = 30;

            text = "Intersected Tiles:";

            this.spriteBatch.DrawString(this.spriteFont,
                                    text,
                                    new Vector2(30, offSetY),
                                    Color.Chartreuse);

            offSetY += (int)spriteFont.MeasureString(text).Y;

            if (intersectedTiles != null)
            {
                foreach (Tile t in this.intersectedTiles)
                {
                    text = t.ToString();

                    this.spriteBatch.DrawString(this.spriteFont,
                                    text,
                                    new Vector2(50, offSetY),
                                    Color.Chartreuse);

                    offSetY += (int)spriteFont.MeasureString(text).Y;

                }
            }

            this.spriteBatch.Draw(this.avtText, this.avtPos, new Color(255, 255, 255, 150));

            this.spriteBatch.DrawString(
                this.spriteFont, 
                "Layer " + curLayer, 
                new Vector2(300, 50), 
                Color.White, 0, Vector2.Zero, 3, SpriteEffects.None, 0);

            this.spriteBatch.DrawString(
                this.spriteFont,
                "Press Left or Right Arrow to change the Layer!",
                new Vector2(48, 540),
                Color.White, 0, Vector2.Zero, 2, SpriteEffects.None, 0);

            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
